﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Configuration;
using System.Xml;
using Application.Services;
using Domaine.Contracts;
using Infrastructure.Data.Repository;
using Infrastructure.Data.UnitOfWork;
using Microsoft.VisualStudio.TestTools.UnitTesting;


namespace Application.MembershipModule.test
{
     [TestClass]
    public class AppMemberShiProviderTests
    {

         [TestMethod]
         public void InitializeTest()
         {

             MembershipSection section = WebConfigurationManager.GetSection("system.web/membership") as MembershipSection;

             int pwlength = Convert.ToInt32(section.Providers[section.DefaultProvider].Parameters["minRequiredPasswordLength"]);

             Assert.IsTrue(pwlength != 0);
         }

         [TestMethod]
         public void AddNewMemberTest()
         {
             //Arrange
             var appService = Init();
             string name="todelete";
             Domaine.Entity.Membership member = new Domaine.Entity.Membership()
             {
                 User = new Domaine.Entity.User()
                 {
                     UserName = name
                 },
                 Email = name + "@test.com",
                 Password = name+"123"
             };

             //Act
             var newMember = appService.AddNewMember(member);

             //Assert
             Assert.IsNotNull(newMember);
             Assert.IsNotNull(newMember.User);
             Assert.IsTrue(newMember.User.UserName == name);

         }
         [TestMethod]
         public void RemoveMeberTest()
         {
             //Arrange
             var appService = Init();
             //Act
             var Test = appService.RemoveMember("md1rw34hsyi");
             //Assert
             Assert.IsTrue(Test);
         }

         [TestMethod]
         public void ChangePasswordTest()
         {
             //Arrange
             var appService = Init();
             //Act
             var Test = appService.ChangePassword("todelete", "todelete123","todelete");
             var valid = appService.ValidateUser("todelete", "todelete");
             //Assert
             Assert.IsTrue(Test);
             Assert.IsTrue(valid);
         }










         private IAppMembershipProvider Init(){
             var unitOfWork = new MembershipUnitOfWork();
             IMemberShipRepository memberShipRepository = new MemberShipRepository(unitOfWork);
             IAspApplicationRepository aspApplicationRepository = new AspApplicationRepository(unitOfWork);
             return new AppMembershipProvider(memberShipRepository, aspApplicationRepository);

         }
         string RandomStr()
         {
             string rStr = System.IO.Path.GetRandomFileName();
             rStr = rStr.Replace(".", ""); // For Removing the .
             return rStr;
         }
    }
}
